import java.io.*;
import java.util.*;

public class ItemList extends ArrayList<Item> {

    private AscendingNameComparator ascendingNameComparator;
    private DescendingNameComparator descendingNameComparator; 
    private DescendingIDComparator descendingIDComparator;
    private AscendingQuantityComparator ascendingQuantityComparator;
    private DescendingQuantityComparator descendingQuantityComparator;

    public ItemList() {
	super();
	ascendingNameComparator = new AscendingNameComparator();
	descendingNameComparator = new DescendingNameComparator();
	descendingIDComparator = new DescendingIDComparator();
	ascendingQuantityComparator = new AscendingQuantityComparator();
	descendingQuantityComparator = new DescendingQuantityComparator();
    }

    public String toString() {
	String s = "";

	s += ("\n\n");
	s += ("                ITEMS");
	s += ("\n---------------------------------------------------------------------\n");
	s += String.format("%-9s %-18s %-11s","ItemID","Item Name", "Quantity");
	s += ("\n---------------------------------------------------------------------\n");
	for(int i = 0; i < size(); i++) {
	    s += String.format("%-9s %-18s %-11s",get(i).itemID,get(i).getProductName(), get(i).getQuantity());
	    s += "\n";
	}
	return s;
    }

    public void sortByIDA() {
	Collections.sort(this);
    }

    public void sortByIDD() {
	Collections.sort(this, descendingIDComparator);
    }

    public void sortByNameA() {
	Collections.sort(this, ascendingNameComparator);
    }
    
    public void sortByNameD() {
	Collections.sort(this, descendingNameComparator);
    }
    public void sortByQuantityA() {
	Collections.sort(this, ascendingQuantityComparator);
    }
    public void sortByQuantityD() {
	Collections.sort(this, descendingQuantityComparator);
    }

    public class DescendingIDComparator implements Serializable, Comparator<Item>{
	public int compare(Item a, Item b) {
	    return (b.itemID - a.itemID);
	}
    }

    public class AscendingNameComparator implements Serializable, Comparator<Item>{
	public int compare(Item a, Item b) {
	    return (a.getProductName().compareTo(b.getProductName()));
	}
    }

    public class DescendingNameComparator implements Serializable, Comparator<Item>{
	public int compare(Item a, Item b) {
	    return (-1*(a.getProductName().compareTo(b.getProductName())));
	}
    }
    public class AscendingQuantityComparator implements Serializable, Comparator<Item>{
	public int compare(Item a, Item b) {
	    return (a.getQuantity()-(b.getQuantity()));
	}
    }

    public class DescendingQuantityComparator implements Serializable, Comparator<Item>{
	public int compare(Item a, Item b) {
	return (b.getQuantity()-(a.getQuantity()));
	}
    }

}